package com.maycari.captainbi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maycari.captainbi.common.constant.CaptainBIConstant;
import com.maycari.captainbi.data.po.Captain3Po;
import com.maycari.captainbi.listener.SyncDto;
import com.maycari.captainbi.listener.SyncListener;
import com.maycari.captainbi.mapper.Captain3Dao;
import com.maycari.captainbi.service.Captain3Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maycari.tool.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王宏伟
 * @since 2021-04-01
 */
@Service
public class Captain3ServiceImpl extends ServiceImpl<Captain3Dao, Captain3Po> implements Captain3Service {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public boolean sync(Integer authorizationId, Date startTime, Date endTime) {
        SyncDto dto = new SyncDto();
        dto.setAuthorizationId(authorizationId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dto.setBeanId("captain3ServiceImpl");
        TreeMap<String, String> params = new TreeMap<>();
        params.put("start_time", simpleDateFormat.format(startTime));
        params.put("end_time", simpleDateFormat.format(endTime));
        dto.setUrl(CaptainBIConstant.CaptainBIUrl.url_3.value());
        dto.setParams(params);
        jmsTemplate.convertAndSend(SyncListener.SYNC_LISTENER, dto);
        return true;
    }

    @Override
    public boolean insertBatch(JSONArray jsonArray) {


        List<Captain3Po> addList = new ArrayList<>();
        List<Captain3Po> updateList = new ArrayList<>();
        List<Captain3Po> Captain3Pos = coverData(jsonArray);

        for (Captain3Po captain3Po : Captain3Pos) {
            LambdaQueryWrapper<Captain3Po> la = new LambdaQueryWrapper<>();
            la.eq(Captain3Po::getId, captain3Po.getId());
            if (count(la) == 0) {
                addList.add(captain3Po);
            } else {
                updateList.add(captain3Po);
            }
        }

        if (CollectionUtil.isNotEmpty(addList)) {
            saveBatch(addList);
        }

        if (CollectionUtil.isNotEmpty(updateList)) {
            updateBatchById(updateList);
        }
        return true;
    }

    private List<Captain3Po> coverData(JSONArray jsonArray) {
        List<Captain3Po> collect = jsonArray.stream().map(arr -> {
            JSONObject v = (JSONObject) arr;
            Captain3Po dto = new Captain3Po();
            dto.setId(v.getLong("id"));
            dto.setSiteId(v.getInteger("site_id"));
            dto.setInventoryId(v.getLong("inventory_id"));
            dto.setPInventoryId(v.getLong("p_inventory_id"));
            dto.setAmazonGoodsId(v.getLong("amazon_goods_id"));
            dto.setFnsku(v.getString("fnsku"));
            dto.setCreateTime(v.getInteger("create_time"));
            dto.setModifiedTime(v.getInteger("modified_time"));
            dto.setDaySale(v.getBigDecimal("day_sale"));
            dto.setDaySale1(v.getInteger("_1_day_sale"));
            dto.setDaySale3(v.getInteger("_3_day_sale"));
            dto.setDaySale7(v.getInteger("_7_day_sale"));
            dto.setDaySale14(v.getInteger("_14_day_sale"));
            dto.setDaySale30(v.getInteger("_30_day_sale"));
            dto.setDaySale60(v.getInteger("_60_day_sale"));
            dto.setDaySale90(v.getInteger("_90_day_sale"));
            dto.setAvgSale3(v.getBigDecimal("3_avg_sale"));
            dto.setAvgSale7(v.getBigDecimal("7_avg_sale"));
            dto.setAvgSale14(v.getBigDecimal("14_avg_sale"));
            dto.setAvgSale30(v.getBigDecimal("30_avg_sale"));
            dto.setAvgSale60(v.getBigDecimal("60_avg_sale"));
            dto.setAvgSale90(v.getBigDecimal("90_avg_sale"));
            dto.setDaySaleReturn1(v.getInteger("_1_day_sale_return"));
            dto.setDaySaleReturn3(v.getInteger("_3_day_sale_return"));
            dto.setDaySaleReturn7(v.getInteger("_7_day_sale_return"));
            dto.setDaySaleReturn14(v.getInteger("_14_day_sale_return"));
            dto.setDaySaleReturn30(v.getInteger("_30_day_sale_return"));
            dto.setDaySaleReturn60(v.getInteger("_60_day_sale_return"));
            dto.setDaySaleReturn90(v.getInteger("_90_day_sale_return"));
            dto.setAvgSaleReturn3(v.getBigDecimal("3_avg_sale_return"));
            dto.setAvgSaleReturn7(v.getBigDecimal("7_avg_sale_return"));
            dto.setAvgSaleReturn14(v.getBigDecimal("14_avg_sale_return"));
            dto.setAvgSaleReturn30(v.getBigDecimal("30_avg_sale_return"));
            dto.setAvgSaleReturn60(v.getBigDecimal("60_avg_sale_return"));
            dto.setAvgSaleReturn90(v.getBigDecimal("90_avg_sale_return"));
            dto.setDaySaleReturn(v.getBigDecimal("day_sale_return"));
            dto.setRavg3(v.getBigDecimal("ravg3"));
            dto.setRavg7(v.getBigDecimal("ravg7"));
            dto.setRavg14(v.getBigDecimal("ravg14"));
            dto.setRavg30(v.getBigDecimal("ravg30"));
            dto.setRavg60(v.getBigDecimal("ravg60"));
            dto.setRavg90(v.getBigDecimal("ravg90"));
            dto.setRavgDay(v.getBigDecimal("ravg_day"));
            dto.setOwnPrice(v.getBigDecimal("own_price"));
            dto.setOwnShipPrice(v.getBigDecimal("own_ship_price"));
            dto.setShipPrice(v.getBigDecimal("ship_price"));
            dto.setShipPriceTime(v.getInteger("ship_price_time"));
            dto.setStatus(v.getInteger("status"));
            dto.setQuantityForLocalFulfillment(v.getInteger("quantity_for_local_fulfillment"));
            dto.setReportModifiedTime(v.getInteger("report_modified_time"));
            dto.setAuthorizationId(v.getInteger("authorizationId"));
            return dto;
        }).collect(Collectors.toList());
        return collect;
    }
}
